<template>
<div>
  <!------------------------面包屑--------------------------->
  <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px">
    <el-breadcrumb-item :to="{path:'/blank'}"><i class="el-icon-s-management"></i>班级管理</el-breadcrumb-item>
    <el-breadcrumb-item :to="{path:'/major'}">专业管理</el-breadcrumb-item>
    <el-breadcrumb-item>添加专业</el-breadcrumb-item>
  </el-breadcrumb>
  <el-divider></el-divider>

  <!------------------------添加题库------------------------>
  <el-card style="margin: 0 auto;width: 500px">
    <h1 style="font-size: 20px">添加专业</h1>
  <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm" style="margin-top: 20px">
    <el-form-item label="专业名称" prop="majorName">
      <el-input v-model="ruleForm.majorName" placeholder="请输入专业"></el-input>
    </el-form-item>
    <el-form-item style="float: left">
      <el-button type="primary" @click="submitForm('ruleForm')">添加</el-button>
      <el-button @click="resetForm('ruleForm')">重置</el-button>
      <el-button type="warning" @click="$router.push('/major')">返回列表</el-button>
    </el-form-item>
  </el-form>
  </el-card>

</div>
</template>

<script>
let localJwt = localStorage.getItem('localJwt');
export default {
  data() {
    return {
      ruleForm: {
        majorName: '',
      },
      rules: {
        majorName: [
          {required: true, message: '请输入名称', trigger: 'blur'},
          {min: 2, max: 25, message: '长度在 2 到 25 个字符', trigger: 'blur'}
        ],
      },
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let url = "http://localhost:9284/major/add-new";
          console.log('url = '+url);
          let formData = this.qs.stringify(this.ruleForm)
          console.log('formData = ' + formData);

          this.axios
              .create({
                'headers': {
                  'Authorization': localJwt
                }
              })
              .post(url,formData).then((response) =>{
            let jsonResult = response.data;
            const h = this.$createElement;

            if (jsonResult.state == 20000){
              this.$notify({
                title: '添加成功',
                message: h('i', { style: 'color: teal'}, jsonResult.message),
                type: 'success'
              });
              this.resetForm(formName);
              if (jsonResult.jwt!=null){
                localStorage.setItem("localJwt",jsonResult.jwt);
              }
            }else if(jsonResult.state==70100){
              this.$alert(jsonResult.message, '错误', {
                confirmButtonText: '确定',
                callback: action => {
                }
              });
              this.$router.push('/')
            } else {
              this.$notify.error({
                title: '添加失败',
                message: h('i', { style: 'color: red'}, jsonResult.message),
              });
            }
          })
          console.log('添加完毕！')
        } else {
          console.log('添加失败!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
}
</script>

<style scoped>

</style>
